package us.mooresr.aircraftscheduling.client.ui;

import us.mooresr.aircraftscheduling.client.LoginStateManager;
import us.mooresr.aircraftscheduling.shared.model.User;
import us.mooresr.aircraftscheduling.shared.model.UserDetail;

import com.gwtplatform.mvp.client.PopupViewImpl;
import com.gwtplatform.mvp.client.PopupViewWithUiHandlers;
import com.gwtplatform.mvp.client.ViewImpl;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

public class UserDetailView extends PopupViewWithUiHandlers<UserDetailUiHandlers> implements
		UserDetailPresenter.MyView {

	private final PopupPanel widget;
	private final LoginStateManager loginStateManager;
	
	private Long currentUserId;
	
	@UiField
	TextBox nameTextBox;
	
	@UiField
	TextBox usernameTextBox;
	
	@UiField
	PasswordTextBox passwordTextBox;
	
	@UiField
	PasswordTextBox confirmPasswordTextBox;
	
	@UiField
	TextBox emailTextBox;
	
	@UiField
	TextBox address1TextBox;
	
	@UiField
	TextBox address2TextBox;
	
	@UiField
	TextBox cityTextBox;
	
	@UiField
	TextBox stateTextBox;
	
	@UiField
	TextBox zipTextBox;
	
	@UiField
	TextBox homePhoneTextBox;
	
	@UiField
	TextBox workPhoneTextBox;
	
	@UiField
	TextBox cellPhoneTextBox;
	
	@UiField
	CheckBox adminCheckBox;
	
	@UiField
	Label adminLabel;
	
	@UiField
	CheckBox notifyNew;
	
	@UiField
	CheckBox notifyDeleted;
	
	@UiField
	Label inactiveLabel;
	
	@UiField
	CheckBox inactiveCheckBox;

	public interface Binder extends UiBinder<PopupPanel, UserDetailView> {
	}

	@Inject
	public UserDetailView(final EventBus eventBus, final Binder binder,
			final LoginStateManager loginStateManager) {
		super(eventBus);
		
		currentUserId = null;
		
		widget = binder.createAndBindUi(this);
		widget.setGlassEnabled(true);
		
		this.loginStateManager = loginStateManager;
	}

	@Override
	public Widget asWidget() {
		return widget;
	}

	@Override
	public void clear() {
		nameTextBox.setText("");
		usernameTextBox.setText("");
		passwordTextBox.setText("");
		confirmPasswordTextBox.setText("");
		emailTextBox.setText("");
		address1TextBox.setText("");
		address2TextBox.setText("");
		cityTextBox.setText("");
		stateTextBox.setText("");
		zipTextBox.setText("");
		homePhoneTextBox.setText("");
		workPhoneTextBox.setText("");
		cellPhoneTextBox.setText("");
		adminCheckBox.setValue(false);
		notifyNew.setValue(false);
		notifyDeleted.setValue(false);
		inactiveCheckBox.setValue(false);
		
		currentUserId = null;
		
		
	}
	
	@UiHandler("cancelButton")
	void onCancelClicked(ClickEvent event) {
		widget.hide();
	}
	
	@UiHandler("okButton")
	void onOkClicked(ClickEvent event) {
		
		
		// The Name and Username must be non-blank
		if (nameTextBox.getText().length() == 0) {
			Window.alert("Name is required");
			return;
		}
		
		if (usernameTextBox.getText().length() == 0) {
			Window.alert("Username is required");
			return;
		}
		
		// The password must be non-blank if this is an add
		if (currentUserId == null) {
			if (passwordTextBox.getText().length() == 0) {
				Window.alert("Password is required");
				return;
			}
		}
		
		// The password must match the confirmPassword
		if (!(passwordTextBox.getText().equals(confirmPasswordTextBox.getText()))) {
			Window.alert("The two passwords entered don't match");
			return;
		}
		

		
		
		User newUser = new User(null, nameTextBox.getText(), inactiveCheckBox.getValue());
		// TODO: Add booleans for email notification
		UserDetail newUserDetail = new UserDetail(null, 
				usernameTextBox.getText(), passwordTextBox.getText(), emailTextBox.getText(),
				address1TextBox.getText(), address2TextBox.getText(), cityTextBox.getText(),
				stateTextBox.getText(), zipTextBox.getText(), homePhoneTextBox.getText(),
				workPhoneTextBox.getText(), cellPhoneTextBox.getText(), adminCheckBox.getValue(),
				notifyNew.getValue(), notifyDeleted.getValue());
		
		// Determine if this is an add or an update
		if (currentUserId == null) {
			// Add
		
			getUiHandlers().createUser(newUser, newUserDetail);
		} else {
			// Update
			newUser.setUserId(currentUserId);
			newUserDetail.setId(currentUserId);
			
			getUiHandlers().updateUser(newUser, newUserDetail);
		}
		
		widget.hide();
		
	}


	@Override
	public void preLoad(User user, UserDetail userDetail) {
		currentUserId = user.getUserId();
		nameTextBox.setText(user.getName());
		usernameTextBox.setText(userDetail.getUsername());
		emailTextBox.setText(userDetail.getEmail());
		address1TextBox.setText(userDetail.getAddress());
		address2TextBox.setText(userDetail.getAddress2());
		cityTextBox.setText(userDetail.getCity());
		stateTextBox.setText(userDetail.getState());
		zipTextBox.setText(userDetail.getZip());
		homePhoneTextBox.setText(userDetail.getHomePhone());
		workPhoneTextBox.setText(userDetail.getWorkPhone());
		cellPhoneTextBox.setText(userDetail.getCellPhone());
		adminCheckBox.setValue(userDetail.getIsAdmin());
		notifyNew.setValue(userDetail.getNotifyOnNewReservation());
		notifyDeleted.setValue(userDetail.getNotifyOnDeletedReservation());
		inactiveCheckBox.setValue(user.getIsInactive());
		
		if (loginStateManager.isAdmin()) {
			adminCheckBox.setVisible(true);
			adminLabel.setVisible(true);
			inactiveCheckBox.setVisible(true);
			inactiveLabel.setVisible(true);
		} else {
			adminCheckBox.setVisible(false);
			adminLabel.setVisible(false);
			inactiveCheckBox.setVisible(false);
			inactiveLabel.setVisible(false);
		}
		
	}
	

	
}
